package com.seelyn.ddvideo.service;

import com.seelyn.ddvideo.model.Video;
import com.seelyn.ddvideo.repository.VideoRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ifcousing-linfeng
 * @since 2017/6/15
 */
@Service
@CacheConfig(cacheNames = "video_cache")
public class VideoService {

    @Autowired
    private VideoRepository videoRepository;

    @CacheEvict()
    public Video saveVideo(Video video) {
        return videoRepository.save(video);
    }

    @Cacheable(key = "'findVideoById_' + #p0")
    public Video findVideoById(String id) {
        return videoRepository.findVideoById(id);
    }

    public Video findVideoByTitle(String title) {
        return videoRepository.findVideoByTitle(title);
    }

    public Video findVideoByFromSiteAndTitle(String fromSite, String title) {
        return videoRepository.findVideoByFromSiteAndTitle(fromSite, title);
    }

    @Cacheable(key = "'findVideoByMovieType_' + #p0 + '_' + #p1.pageNumber + '_' + #p1.pageSize")
    public Page<Video> findVideoByMovieType(Integer movieType, Pageable pageable) {
        return videoRepository.findVideoByMovieType(movieType, pageable);
    }

    @Cacheable(key = "'findVideoByMovieTypeLessThanEqual_' + #p0 + '_' + #p1.pageNumber + '_' + #p1.pageSize")
    public Page<Video> findVideoByMovieTypeLessThanEqual(Integer movieType, Pageable pageable) {
        return videoRepository.findVideoByMovieTypeLessThanEqual(movieType, pageable);
    }

    @Cacheable(key = "'findVideoByTitleLikeAndMovieTypeLessThanEqual_' + #p0 + '_' + #p1 + '_' + #p2.pageNumber + '_' + #p2.pageSize")
    public Page<Video> findVideoByTitleLikeAndMovieTypeLessThanEqual(String title, Integer movieType, Pageable pageable) {
        return videoRepository.findVideoByTitleLikeAndMovieTypeLessThanEqual(title, movieType, pageable);
    }

    @Cacheable(key = "'findVideoByTitleLikeAndMovieTypeLessThanEqualAndSubType_' + #p0 + '_' + #p1 + '_' +  #p2 + '_' + #p3.pageNumber + '_' + #p3.pageSize")
    public Page<Video> findVideoByTitleLikeAndMovieTypeLessThanEqualAndSubType(String title, Integer movieType, Integer subTypes, Pageable pageable) {
        return videoRepository.findVideoByTitleLikeAndMovieTypeLessThanEqualAndSubType(title, movieType, subTypes, pageable);
    }

    @Cacheable(key = "'findVideoByTitleLike_' + #p0 + '_' + #p1.pageNumber + '_' + #p1.pageSize")
    public Page<Video> findVideoByTitleLike(String title, Pageable pageable) {
        return videoRepository.findVideoByTitleLike(title, pageable);
    }

    public Video findVideoByFromSiteAndVid(String fromSite, String vid) {
        return videoRepository.findVideoByFromSiteAndVid(fromSite, vid);
    }

    @Cacheable(key = "'findVideoByFromSiteOrderByScore_' + #p0")
    public List<Video> findVideoByFromSiteOrderByScore(String fromSite) {
        return videoRepository.findVideoByFromSiteOrderByScore(fromSite);
    }

    @Cacheable(key = "'findVideoByFromSiteAndMovieTypeOrderByScore_' + #p0 + '_' + #p1")
    public List<Video> findVideoByFromSiteAndMovieTypeOrderByScore(String fromSite, Integer type) {
        return videoRepository.findVideoByFromSiteAndMovieTypeOrderByScore(fromSite, type);
    }

    @Cacheable(key = "'findVideoByFromSiteAndMovieType_' + #p0 + '+' + #p1 + '_' + #p2.pageNumber + '_' + #p2.pageSize")
    public Page<Video> findVideoByFromSiteAndMovieType(String fromSite, Integer type, Pageable pageable) {
        return videoRepository.findVideoByFromSiteAndMovieType(fromSite, type, pageable);
    }

    @Cacheable(key = "'findVideoByFromSiteAndMovieTypeOrderByScore_' + #p0 + '+' + #p1 + '_' + #p2.pageNumber + '_' + #p2.pageSize")
    public Page<Video> findVideoByFromSiteAndMovieTypeOrderByScore(String fromSite, Integer type, Pageable pageable) {
        return videoRepository.findVideoByFromSiteAndMovieTypeOrderByScore(fromSite, type, pageable);
    }

    @Cacheable(key = "'findByFromSiteAndMovieTypeAndDelFlagFalse_' + #p0 + '+' + #p1 + '_' + #p2.pageNumber + '_' + #p2.pageSize")
    public Page<Video> findByFromSiteAndMovieTypeAndDelFlagFalse(String fromSite, Integer type, Pageable pageable) {
        return videoRepository.findByFromSiteAndMovieTypeAndDelFlagFalse(fromSite, type, pageable);
    }

    @Cacheable(key = "'findByMovieTypeAndDelFlagFalse_' + #p0  + '_' + #p1.pageNumber + '_' + #p1.pageSize")
    public Page<Video> findByMovieTypeAndDelFlagFalse(Integer type, Pageable pageable) {
        return videoRepository.findByMovieTypeAndDelFlagFalse(type, pageable);
    }

    @Cacheable(key = "'findVideoByFromSiteOrderByScore_' + #p0 + '_' + #p1.pageNumber + '_' + #p1.pageSize")
    public Page<Video> findVideoByFromSiteOrderByScore(String fromSite, Pageable pageable) {
        return videoRepository.findVideoByFromSiteOrderByScore(fromSite, pageable);
    }


}
